/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2008, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.

* Neither the copyright holder's name nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*------------------------------ TAVLTree.h -------------------------------*/
/*===========================================================================
FILE: TAVLTree.h
Author: Wangxing SHI (swxlion)
Version: 0.0.3 
Date: 2008-03-29

File Description:
    C++ template for the AVL Tree.
    This version using both iteration and recursion achieve.

Require:
    Overload the "<", ">", "==", "=" four operators for type.

Dependencies:
    Platform: Platform Independent.

NOTE:
	If using with the Memory Pool, please DO NOT use the fuanction Clear(),
	Remove( , true), Delete(). And before destroy instance the tree, please ensure
	the tree is empty, because the instance will call the Clear() function in
	the destructor.

History:
version        author        date       description
----------------------------------------------------
0.0.1          swxlion       06-08-08   Created. 
0.0.2          swxlion       07-08-08   Modify.
0.0.3          swxlion       08-03-29   Integrated in the ASDL namespace.
===========================================================================*/
#ifndef _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TAVL_TREE_H_
#define _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TAVL_TREE_H_

/*===============================================================================
CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace ASDL
{
	template <typename Type>
	class TAVLTree
	{
	public:
		struct AVLNode
		{
			Type		Data;
			AVLNode		*Left;
			AVLNode		*Right;
			AVLNode		*Parent;
			int			Balance;

			AVLNode(): Left(0), Right(0), Parent(0), Balance(0)
			{
			}
			AVLNode( const Type &d ): Data(d), Left(0), Right(0), Parent(0), Balance(0)
			{
			}
		};

	protected:
		AVLNode		*pRoot;

		void		RotateLeft( AVLNode * pTree, AVLNode * &pNewTree );		//-- Left alone rotation.
		void		RotateRight( AVLNode * pTree, AVLNode * &pNewTree );	//-- Right alone rotation.
		void		LeftBalance( AVLNode * &pTree );						//-- Left balance (double rotation)
		void		RightBalance( AVLNode * &pTree );						//-- Right balance (double rotation)
		int			Insert( AVLNode * &pTree, AVLNode * &pNode, int & taller, bool bRelpace = true ); //-- Recursion version.
		void		Remove( AVLNode *&pTree, const AVLNode * &pNode, int &inc );	//-- Recursion version.
		void		LeftAdjust_Del( AVLNode * &pTree, int &inc );
		void		RightAdjust_Del( AVLNode * &pTree, int &inc );

		AVLNode *	FindMin( AVLNode * pNode )
		{
			while( pNode->Left )
				pNode = pNode->Left;

			return pNode;
		}

		AVLNode *	FindMax( AVLNode * pNode )
		{
			while( pNode->Right )
				pNode = pNode->Right;

			return pNode;
		}

	public:
		TAVLTree(): pRoot(0)
		{
		}
		~TAVLTree()
		{
			Clear();
		}

		AVLNode *	Find( AVLNode * pNode )
		{
			if( !pRoot )
				return NULL;

			AVLNode*	pFind = pRoot;
			while( pFind )
			{
				if( pFind->Data == pNode->Data )
					return pFind;
				else if( pFind->Data > pNode->Data )
					pFind = pFind->Left;
				else
					pFind = pFind->Right;
			}
			return NULL;
		}

		AVLNode *	Find( Type *pT )
		{
			if( !pRoot )
				return NULL;

			AVLNode*	pFind = pRoot;
			while( pFind )
			{
				if( pFind->Data == *pT )
					return pFind;
				else if( pFind->Data > *pT )
					pFind = pFind->Left;
				else
					pFind = pFind->Right;
			}
			return NULL;
		}

		AVLNode *	Find( const Type &T )
		{
			if( !pRoot )
				return NULL;

			AVLNode*	pFind = pRoot;
			while( pFind )
			{
				if( pFind->Data == T )
					return pFind;
				else if( pFind->Data > T )
					pFind = pFind->Left;
				else
					pFind = pFind->Right;
			}
			return NULL;
		}

		AVLNode *	FindMin()												// Just apply to children, not apply the whole tree.
		{
			if( pRoot == NULL )
				return NULL;

			return FindMin(pRoot);
		}

		AVLNode *	FindMax()												// Just apply to children, not apply the whole tree.
		{
			if( pRoot == NULL )
				return NULL;

			return FindMax(pRoot);
		}

		int			Insert( AVLNode * pNode, bool bReplace = true );		//-- Iteration version.
		void		Remove( AVLNode * &pNode, bool bDelete = false );		//-- Iteration version.

		void		Remove( const Type &T, bool bDelete = false  )
		{
			AVLNode	*pNode = Find(T);
			Remove( pNode, bDelete );
		}

		void		Delete( AVLNode * &pNode )
		{
			Remove( pNode, true );
		}

		void		Delete( const Type &T )
		{
			AVLNode	*pNode = Find(T);
			Remove( pNode, true );
		}

		void		Clear();

		int			Insert_R( AVLNode * &pNode, bool bRelpace = true )		//-- Recursion version.
		{
			pNode->Balance = 0;
			pNode->Parent = NULL;
			pNode->Left = NULL;
			pNode->Right = NULL;

			int taller;
			return Insert( pRoot, pNode, taller, bRelpace );
		}
		void		Remove_R( AVLNode * &pNode, bool bDelete = false )		//-- Recursion version.
		{
			int inc = 0;
			Remove( pRoot, pNode, inc );

			if( bDelete )
			{
				delete pNode;
				pNode = NULL;
			}
		}

		AVLNode *	GetNext( AVLNode * pNode )
		{
			if( !pNode )
				return NULL;

			AVLNode * pFindNode = NULL;
			AVLNode * pParentNode = pNode->Right;

			if( pParentNode )
			{
				pFindNode = pParentNode->Left;
				while( pFindNode )
				{
					pParentNode = pFindNode;
					pFindNode = pParentNode->Left;
				}
				return pParentNode;
			}
			else
			{
				pFindNode = pNode;
				while( 1 )
				{
					pParentNode = pFindNode->Parent;
					if( !pParentNode )
						return NULL;
					if( pParentNode->Left == pFindNode )
						return pParentNode;
					pFindNode = pParentNode;
				}
			}

			return NULL;
		}

		AVLNode *	GetPrevious( AVLNode * pNode )
		{
			if( !pNode )
				return NULL;

			AVLNode * pFindNode = NULL;
			AVLNode * pParentNode = pNode->Left;

			if( pParentNode )
			{
				pFindNode = pParentNode->Right;
				while( pFindNode )
				{
					pParentNode = pFindNode;
					pFindNode = pParentNode->Right;
				}
				return pParentNode;
			}
			else
			{
				pFindNode = pNode;
				while( 1 )
				{
					pParentNode = pFindNode->Parent;
					if( !pParentNode )
						return NULL;
					if( pParentNode->Right == pFindNode )
						return pParentNode;
					pFindNode = pParentNode;
				}
			}

			return NULL;
		}

		AVLNode *	FindLarger( const Type &T )
		{
			if( !pRoot )
				return NULL;

			AVLNode*	pFind = pRoot;
			AVLNode*	pLastFind = NULL;
			while( pFind )
			{
				if( pFind->Data > T )
				{
					pLastFind = pFind;
					pFind = pFind->Left;
				}
				else	//-- pFind->Data <= T
					pFind = pFind->Right;
			}
			return pLastFind;
		}

		AVLNode *	FindSmaller( const Type &T )
		{
			if( !pRoot )
				return NULL;

			AVLNode*	pFind = pRoot;
			AVLNode*	pLastFind = NULL;
			while( pFind )
			{
				if( pFind->Data < T )
				{
					pLastFind = pFind;
					pFind = pFind->Right;
				}
				else	//-- pFind->Data >= T
					pFind = pFind->Left;
			}
			return pLastFind;
		}
	};

	/*===============================================================================
	FUNCTION DEFINITIONS: AVL Tree Functions
	=============================================================================== */
	/*===========================================================================

	FUNCTION: TAVLTree<Type>::RotateLeft

	DESCRIPTION:
	Left alone rotation.

	PARAMETERS:
	pTree [in] - The subtree require to be adjusted.
	NewTree [out] - The subtree adjusted.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::RotateLeft( AVLNode * pTree, AVLNode * &pNewTree )
	{
		pNewTree = pTree->Right;
		pNewTree->Parent = pTree->Parent;
		pTree->Right = pNewTree->Left;
		if( pNewTree->Left )
			pNewTree->Left->Parent = pTree;
		pNewTree->Left = pTree;
		pTree->Parent = pNewTree;
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::RotateRight

	DESCRIPTION:
	Right alone rotation.

	PARAMETERS:
	pTree [in] - The subtree require to be adjusted.
	NewTree [out] - The subtree adjusted.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::RotateRight( AVLNode * pTree, AVLNode * &pNewTree )
	{
		pNewTree = pTree->Left;
		pNewTree->Parent = pTree->Parent;
		pTree->Left = pNewTree->Right;
		if( pNewTree->Right )
			pNewTree->Right->Parent = pTree;
		pNewTree->Right = pTree;
		pTree->Parent = pNewTree;
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::LeftBalance

	DESCRIPTION:
	Left balance. (double rotation)

	PARAMETERS:
	pTree [in, out] - [in] The subtree require to be adjusted.
	                  [out] The subtree adjusted.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::LeftBalance( AVLNode * &pTree )
	{
		AVLNode *		pLeftSub = pTree->Left;
		AVLNode *		pRightSub = 0;

		switch( pLeftSub->Balance )
		{
		case -1:
			pTree->Balance = pLeftSub->Balance = 0;
			RotateRight( pTree, pTree );
			return;
		case 0:
			return;			//-- Tree already balanced.
		case 1:
			pRightSub = pLeftSub->Right;
			switch( pRightSub->Balance )
			{
			case -1:
				pTree->Balance = 1;
				pLeftSub->Balance = 0;
				break;
			case 0:
				pTree->Balance = pLeftSub->Balance = 0;
				break;
			case 1:
				pTree->Balance = 0;
				pLeftSub->Balance = -1;
				break;
			}

			pRightSub->Balance = 0;
			RotateLeft( pLeftSub, pTree->Left );
			RotateRight( pTree, pTree );
		}
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::RightBalance

	DESCRIPTION:
	Right balance. (double rotation)

	PARAMETERS:
	pTree [in, out] - [in] The subtree require to be adjusted.
	                  [out] The subtree adjusted.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::RightBalance( AVLNode * &pTree )
	{
		AVLNode * pRightSub = pTree->Right;
		AVLNode * pLeftSub = 0;
		switch( pRightSub->Balance )
		{
		case 1:
			pTree->Balance = pRightSub->Balance = 0;
			RotateLeft( pTree, pTree );
			return;
		case 0:
			return;			//-- Tree already balanced.
		case -1:
			pLeftSub = pRightSub->Left;
			switch( pLeftSub->Balance )
			{
			case 1:
				pTree->Balance = -1;
				pRightSub->Balance = 0;
				break;
			case 0:
				pTree->Balance = pRightSub->Balance = 0;
				break;
			case -1:
				pTree->Balance = 0;
				pRightSub->Balance = 1;
				break;
			}

			pLeftSub->Balance = 0;
			RotateRight( pRightSub, pTree->Right );
			RotateLeft( pTree, pTree );
		}
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::Insert

	DESCRIPTION:
	Insert new node in the tree. (Iteration version.)

	PARAMETERS:
	pNode    [in] - The new node will be inserted in the tree.
	bReplace [in] - When the data of the pNode has already existed in the tree,
	              "true" will replace the old value, "false" do nothing.

	RETURN VALUE:
	0 - Succeed.
	1 - The data of the pNode has already existed in the tree, replace or not just
	    according the "bReplace" sign.

	NOTICE:
	Only the value of node be replaced, not the instance of the node.
	===========================================================================*/
	template <typename Type>
	int TAVLTree<Type>::Insert( AVLNode * pNode, bool bReplace )
	{
		if( !pNode )
			return 0;

		pNode->Balance = 0;
		pNode->Parent = NULL;
		pNode->Left = NULL;
		pNode->Right = NULL;

		if( !pRoot )
		{
			pRoot = pNode;
			return 0;
		}

		register AVLNode *		pParentNode = pRoot;

		do
		{
			if( pParentNode->Data > pNode->Data )
			{
				if( pParentNode->Left )
					pParentNode = pParentNode->Left;
				else
				{
					pParentNode->Left = pNode;
					pNode->Parent = pParentNode;
					break;
				}
			}
			else if( pParentNode->Data < pNode->Data )
			{
				if( pParentNode->Right )
					pParentNode = pParentNode->Right;
				else
				{
					pParentNode->Right = pNode;
					pNode->Parent = pParentNode;
					break;
				}
			}
			else if( pParentNode->Data == pNode->Data )
			{
				if( bReplace )
					pParentNode->Data = pNode->Data;
				return 1;
			}
		}
		while(1);

		AVLNode *		temp = pNode->Parent;
		while( temp )
		{
			if( temp->Data > pNode->Data )	//-- Inserted in the left branch.
			{
				switch( temp->Balance )
				{
				case -1:
					// LeftBalance( (*ppSearchNode) );
					if( temp->Parent == NULL )
					{
						LeftBalance( temp );
						pRoot = temp;
					}
					else
					{
						AVLNode*	pChildNode = temp;
						pParentNode = temp->Parent;

						LeftBalance( temp );
						if( pParentNode->Left == pChildNode )
							pParentNode->Left = temp;
						else
							pParentNode->Right = temp;
					}
					return 0;
				case 0:
					temp->Balance = -1;
					break;
				case 1:
					temp->Balance = 0;
					return 0;
				}
			}
			else									//-- Inserted in the right branch.
			{
				switch( temp->Balance )
				{
				case -1:
					temp->Balance = 0;
					return 0;
				case 0:
					temp->Balance = 1;
					break;
				case 1:
					// RightBalance( (*ppSearchNode) );
					if( temp->Parent == NULL )
					{
						RightBalance( temp );
						pRoot = temp;
					}
					else
					{
						AVLNode*	pChildNode = temp;
						pParentNode = temp->Parent;

						RightBalance( temp );
						if( pParentNode->Left == pChildNode )
							pParentNode->Left = temp;
						else
							pParentNode->Right = temp;
					}
					return 0;
				}
			}
			temp = temp->Parent;
		}

		return 0;
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::Insert

	DESCRIPTION:
	Insert new node in the tree. (Recursion version.)

	PARAMETERS:
	pTree [in] - The root node of the subtree which will be inserted the new node.
	pNode [in] - The new node will be inserted in the tree.
	taller [in, out] - Adjustment factor.
	bReplace [in] - When the data of the pNode has already existed in the tree,
	              "true" will replace the old value, "false" do nothing.

	RETURN VALUE:
	0 - Succeed.
	1 - The data of the pNode has already existed in the tree, replace or not just
	    according the "bReplace" sign.
	2 - Succeed. Insert at Root.

	NOTICE:
	Only the value of node be replaced, not the instance of the node.
	===========================================================================*/
	template <typename Type>
	int TAVLTree<Type>::Insert( AVLNode * &pTree, AVLNode * &pNode, int & taller, bool bRpelace )
	{
		int iReValue = 2;

		if( pTree == NULL )
		{
			pTree = pNode;
			taller = 1;
		}

		else if( pNode->Data < pTree->Data )
		{
			iReValue = Insert( pTree->Left, pNode,  taller, bRpelace );
			if( iReValue == 2 )
			{
				pNode->Parent = pTree;
				iReValue = 0;
			}
			else if( iReValue == 1 )
				return 1;

			if( taller )
				switch( pTree->Balance )
			{
				case -1:
					LeftBalance( pTree );
					taller = 0;
					break;
				case 0:
					pTree->Balance = -1;
					break;
				case 1:
					pTree->Balance = 0;
					taller = 0;
					break;
			}
		}

		else if( pNode->Data > pTree->Data )
		{
			iReValue = Insert( pTree->Right, pNode,  taller, bRpelace );
			if( iReValue == 2 )
			{
				pNode->Parent = pTree;
				iReValue = 0;
			}
			else if( iReValue == 1 )
				return 1;

			if( taller )
				switch( pTree->Balance )
			{
				case -1:
					pTree->Balance = 0;
					taller = 0;
					break;
				case 0:
					pTree->Balance = 1;
					break;
				case 1:
					RightBalance( pTree );
					taller = 0;
					break;
			}
		}

		else					//-- else if( pNode->Data == pTree->Data )
		{
			if( bRpelace )
				pTree->Data = pNode->Data;
			return 1;
		}

		return iReValue;
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::LeftAdjust_Del

	DESCRIPTION:
	Left balance. (double rotation)

	PARAMETERS:
	pTree [in, out] - [in] The subtree require to be adjusted.
	                  [out] The subtree adjusted.
	inc	[in, out] -	Adjustment factor.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::LeftAdjust_Del( AVLNode * &pTree, int &inc )
	{
		AVLNode * pLeftSub = pTree->Left;
		switch ( pLeftSub->Balance )
		{
		case 0:
			pTree->Balance = -1;
			pLeftSub->Balance = 1;
			//__rightroate(pTree, pLeftSub);
			RotateRight( pTree, pTree );
			inc = 0;
			break;
		case -1:
			pTree->Balance = 0;
			pLeftSub->Balance = 0;
			//__rightroate(pTree, pLeftSub);
			RotateRight( pTree, pTree );
			inc = 1;
			break;
		case 1:
			AVLNode * pRightSub = pLeftSub->Right;
			switch ( pRightSub->Balance )
			{
			case 0:
				pTree->Balance = 0;
				pLeftSub->Balance = 0;
				pRightSub->Balance = 0;
				inc = 1;
				break;
			case 1:
				pTree->Balance = -1;
				pLeftSub->Balance = 0;
				pRightSub->Balance = 0;
				inc = 1;
				break;
			case -1:
				pTree->Balance = 0;
				pLeftSub->Balance = 1;
				pRightSub->Balance = 0;
				inc = 1;
				break;
			}
			//__leftroate( pLeftSub, pRightSub );
			RotateLeft( pLeftSub, pTree->Left );
			//__rightroate(pTree, pLeftSub);
			RotateRight( pTree, pTree );
			break;
		}
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::RightAdjust_Del

	DESCRIPTION:
	Right balance. (double rotation)

	PARAMETERS:
	pTree [in, out] - [in] The subtree require to be adjusted.
	                  [out] The subtree adjusted.
	inc	[in, out] -	Adjustment factor.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::RightAdjust_Del( AVLNode * &pTree, int &inc )
	{
		AVLNode * pRightSub = pTree->Right;
		switch ( pRightSub->Balance )
		{
		case 0:
			pRightSub->Balance = -1;
			pTree->Balance = 1;
			inc = 0;
			//__leftroate( pTree, pRightSub);
			RotateLeft( pTree, pTree );
			break;
		case 1:
			pRightSub->Balance = 0;
			pTree->Balance = 0;
			inc = 1;
			//__leftroate( pTree, pRightSub);
			RotateLeft( pTree, pTree );
			break;
		case -1:
			AVLNode * pLeftSub = pRightSub->Left;
			switch (pLeftSub->Balance)
			{
			case 0:
				pTree->Balance = 0;
				pRightSub->Balance = 0;
				pLeftSub->Balance = 0;
				inc = 1;
				break;
			case 1:
				pTree->Balance = -1;
				pRightSub->Balance = 0;
				pLeftSub->Balance = 0;
				inc = 1;
				break;
			case -1:
				pTree->Balance = 0;
				pRightSub->Balance = 1;
				pLeftSub->Balance = 0;
				inc = 1;
				break;
			}
			//__rightroate( pRightSub, pLeftSub);
			RotateRight( pRightSub, pTree->Right );
			//__leftroate( pTree, pRightSub);
			RotateLeft( pTree, pTree );
			break;
		}
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::Remove

	DESCRIPTION:
	Remove the specified node from the tree. (Recursion version.)

	PARAMETERS:
	pTree [in] - The root node of the subtree which will be removed the specified node.
	pNode [in] - The node will be removed from the tree.
	inc [in, out] - Adjustment factor.

	RETURN VALUE:
	None.
	NOTICE:
	Just only remove, not delete.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::Remove( AVLNode *&pTree, const AVLNode * &pNode, int & inc )
	{
		if ( pTree == NULL )
		{
			inc = 0;
			return;
		}

		else if( pTree->Data < pNode->Data )
		{
			Remove( pTree->Right, pNode, inc );
			if ( inc == 0 )
				return;
			switch ( pTree->Balance )
			{
			case 0:
				pTree->Balance = -1;
				inc = 0;
				break;
			case 1:
				pTree->Balance = 0;
				inc = 1;
				break;
			case -1:
				LeftAdjust_Del( pTree, inc );
				break;
			}
		}

		else if( pTree->Data > pNode->Data )
		{
			Remove( pTree->Left, pNode, inc );
			if ( inc == 0 )
				return;
			switch ( pTree->Balance )
			{
			case 0:
				pTree->Balance = 1;
				inc = 0;
				break;
			case -1:
				pTree->Balance = 0;
				inc = 1;
				break;
			case 1:
				RightAdjust_Del( pTree, inc );
				break;
			}
		}

		else
		{
			if ( pTree->Left && pTree->Right )
			{
				AVLNode *temp = FindMin( pTree->Right );
				Remove( pTree->Right, temp, inc );
				pTree->Data = temp->Data;

				if( inc == 0 )
					return;
				switch ( pTree->Balance )
				{
				case 0:
					pTree->Balance = -1;
					inc = 0;
					break;
				case 1:
					pTree->Balance = 0;
					inc = 1;
					break;
				case -1:
					LeftAdjust_Del( pTree, inc );
					break;
				}

				return;
			}
			//		AVLNode *tmp = pTree;
			pTree = ( pTree->Left != 0 ? pTree->Left : pTree->Right );
			//		delete tmp;
			inc = 1;
			return;
		}
	}

	/*===========================================================================

	FUNCTION: TAVLTree<Type>::Remove

	DESCRIPTION:
	Remove the specified node from the tree. (Iteration version.)

	PARAMETERS:
	pNode   [in] - The node will be removed from the tree.
	bDelete [in] - true: Remove and delete the specified node;
	               false: Just only remove, but not delete.

	RETURN VALUE:
	None.
	===========================================================================*/
	template <typename Type>
	void TAVLTree<Type>::Remove( AVLNode * &pNode, bool bDelete )
	{
		if( !pNode )
			return;

		int		inc = 0;
		//-- Part.1: Adjust the node which will be removed.
		if( pNode ->Left && pNode->Right )
		{
			AVLNode *		temp = FindMin( pNode->Right );			// According the algorithm of the function FindMin, the temp isn't able to have the left-node at the moment.
			pNode->Data = temp->Data;
			pNode = temp;
		}

		//--Part.2: Adjust the pointers.
		AVLNode *		pParentNode = pNode->Parent;		//-- maybe null
		if( !pNode->Parent )			//-- Root node: pRoot.
		{
			pRoot = ( pNode->Left != 0 ? pNode->Left : pNode->Right );
			if( pRoot )
				pRoot->Parent = NULL;
			
			if( bDelete )
			{
				delete pNode;
				pNode = NULL;
			}

			return;
		}

		//--  At the moment, the pNode has only one sub node or not.
		//-- 2007-08-03 --: if( pParentNode->Data > pNode->Data )
		if( pParentNode->Left == pNode )
		{
			pParentNode->Left = ( pNode->Left != 0 ? pNode->Left : pNode->Right );
			if( pParentNode->Left )
				pParentNode->Left->Parent = pParentNode;
		}
		else		//-- Include the status of equal.
		{
			pParentNode->Right = ( pNode->Left != 0 ? pNode->Left : pNode->Right );
			if( pParentNode->Right )
				pParentNode->Right->Parent = pParentNode;
		}
		
		//-- delete pNode;

		//--Part.3: Adjust the balance of the tree.				comment: At the moment, the pNode has only one sub node or not.
		while( pParentNode )
		{
			if( pParentNode->Data > pNode->Data )				//-- Removing happened in left branch.
			{
				switch( pParentNode->Balance )
				{
				case 1:
					//--	RightAdjust_Del( pTree, inc );
					if( pParentNode->Parent == NULL )
					{
						RightAdjust_Del( pParentNode, inc );
						pRoot = pParentNode;
						goto DeleteJudgement;	// return;
					}
					else
					{
						AVLNode*	pChildNode = pParentNode;
						AVLNode*	pCurrNode = pParentNode;
						pParentNode = pParentNode->Parent;

						RightAdjust_Del( pCurrNode, inc );
						if( pParentNode->Left == pChildNode )
							pParentNode->Left = pCurrNode;
						else
							pParentNode->Right = pCurrNode;

						if( inc == 0 )
							goto DeleteJudgement;	// return;
						else
							continue;
					}
					break;
				case 0:
					pParentNode->Balance = 1;
					goto DeleteJudgement;	// return;
				case -1:
					pParentNode->Balance = 0;
					break;
				}
			}
			else								//-- Removing happened in right branch. (Include the status of equal.)
			{
				switch( pParentNode->Balance )
				{
				case 1:
					pParentNode->Balance = 0;
					break;
				case 0:
					pParentNode->Balance = -1;
					goto DeleteJudgement;	// return;
				case -1:
					//---	LeftAdjust_Del( pTree, inc );
					if( pParentNode->Parent == NULL )
					{
						LeftAdjust_Del( pParentNode, inc );
						pRoot = pParentNode;
						goto DeleteJudgement;	// return;
					}
					else
					{
						AVLNode*	pChildNode = pParentNode;
						AVLNode*	pCurrNode = pParentNode;
						pParentNode = pParentNode->Parent;

						LeftAdjust_Del( pCurrNode, inc );
						if( pParentNode->Left == pChildNode )
							pParentNode->Left = pCurrNode;
						else
							pParentNode->Right = pCurrNode;

						if( inc == 0 )
							goto DeleteJudgement;	// return;
						else
							continue;
					}
					break;
				}
			}
			pParentNode = pParentNode->Parent;
		}

DeleteJudgement:
		if( bDelete )
		{
			delete pNode;
			pNode = NULL;
		}
	}

	template <typename Type>
	void TAVLTree<Type>::Clear()
	{
		//-- Too slowly.
		//while( pRoot )
		//	Remove( pRoot, true );

		if( !pRoot )
			return;

		AVLNode			*lpFindNode = pRoot;
		AVLNode			*lpDeleteNode = NULL;

		while( lpFindNode )
		{
			if( lpFindNode->Left )
				lpFindNode = lpFindNode->Left;
			else if( lpFindNode->Right )
				lpFindNode = lpFindNode->Right;
			else
			{
				lpDeleteNode = lpFindNode;
				lpFindNode = lpFindNode->Parent;

				if( lpDeleteNode->Parent ) //-- Now, lpDeleteNode->Parent is lpFindNode. Just for eliminating data dependence.
				{
					if( lpFindNode->Left == lpDeleteNode )
						lpFindNode->Left = NULL;
					else if( lpFindNode->Right == lpDeleteNode )
						lpFindNode->Right = NULL;
				}

				delete lpDeleteNode;
			}
		}
		pRoot = NULL;
	}
}

#endif // _APPLICATION_SERVER_DEVELOPMENT_LIBRARY__TAVL_TREE_H_
